﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MapCenterSystem.DataInfo;
using MapCenterSytem.Process;
using MapCenterSystem.ServiceProvider.GoogleMap;
using MapCenterSystem.Utility;
using MapCenterSystem.DefineType;
using System.Drawing;
using System.Collections.Generic;

using System.Threading;
using System.Resources;
using System.Globalization;


namespace MobiMap.Admin
{
    public partial class placeUC : System.Web.UI.UserControl, System.Web.UI.ICallbackEventHandler
    {

        #region properties
        public string FullName
        {
            get { return txtService_FullName.Value; }
            set { txtService_FullName.Value = value; }
        }

        public string Address
        {
            get { return txtService_Address.Value; }
            set { txtService_Address.Value = value; }
        }

        private string selectedType;
        public string Type
        {
            get { return cmbPlaceType.SelectedValue; }
            set { cmbPlaceType.SelectedValue = value; }
        }

        public string Latitude
        {
            get { return txtService_Latitude.Value; }
            set { txtService_Latitude.Value = value; }
        }

        public string Longitude
        {
            get { return txtService_Longitude.Value; }
            set { txtService_Longitude.Value = value; }
        }

        public string Phone
        {
            get { return txtService_Phone.Value; }
            set { txtService_Phone.Value = value; }
        }

        public string Info
        {
            get { return txtService_Info.Value; }
            set { txtService_Info.Value = value; }
        }

        public string Website
        {
            get { return txtService_Website.Value; }
            set { txtService_Website.Value = value; }
        }

        public HtmlInputFile Picture
        {
            get { return txtService_Picture; }
        }
        #endregion

        ResourceManager rm;
        CultureInfo ci;

        public void FillContent(PlaceInfo placeinfo)
        {
            this.FullName = placeinfo.FullName;
            this.Address = placeinfo.Address;
            this.selectedType = placeinfo.Type.ID.ToString();
            this.Latitude = placeinfo.Location.Latitude.ToString();
            this.Longitude = placeinfo.Location.Longitude.ToString();
            this.Phone = placeinfo.Phone;
            this.Website = placeinfo.Website;
            this.Info = placeinfo.Info;            
        }
        protected void Page_Load(object sender, EventArgs e)
        {          

            //Dang ky su kien callback
            ClientScriptManager cm = Page.ClientScript;
            String cbReference = cm.GetCallbackEventReference(this, "evtArgs",
                                                "GetDataFromServer", "");
            String callbackScript = "function CallServer(evtArgs) {" +
                                            cbReference + "; }";
            cm.RegisterClientScriptBlock(this.GetType(),
                                    "CallServer", callbackScript, true);

            if (!IsPostBack)
            {
                IList<PlaceTypeInfo> lstPlaceTypeInfo = new List<PlaceTypeInfo>();
                lstPlaceTypeInfo = PlaceTypeEx.GetListPlaceType();
                cmbPlaceType.DataSource = lstPlaceTypeInfo;
                cmbPlaceType.DataTextField = "Description";
                cmbPlaceType.DataValueField = "ID";
                cmbPlaceType.DataBind();

                ListItem li = cmbPlaceType.Items.FindByValue(selectedType);
                int idx = cmbPlaceType.Items.IndexOf(li);
                cmbPlaceType.SelectedIndex = idx;


                cmbPlaceType.Attributes.Add("onchange", "changePlaceType();");
            }

            //Language
            SetLanguage();
        }

        public void SetLanguage()
        {
            rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
            if (Request.Cookies["language"] != null)
            {
                string language = Request.Cookies["language"].Value;
                Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
                LoadString(Thread.CurrentThread.CurrentCulture);

                return;
            }

            //Defaut language is Vietnamese
            Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
            ci = Thread.CurrentThread.CurrentCulture;
            LoadString(ci);
        }

        public void LoadString(CultureInfo ci)
        {
            lbPlaceName.InnerHtml = rm.GetString("Admin_PlaceUC_lbPlaceName", ci);
            lbPlaceType.InnerHtml = rm.GetString("Admin_PlaceUC_lbPlaceType", ci);
            lbAddress.InnerHtml = rm.GetString("Admin_PlaceUC_lbAddress", ci);
            lbLatitude.InnerHtml = rm.GetString("Admin_PlaceUC_lbLatitude", ci);
            lbLongitude.InnerHtml = rm.GetString("Admin_PlaceUC_lbLongitude", ci);
            lbPhone.InnerHtml = rm.GetString("Admin_PlaceUC_lbPhone", ci);
            lbWebsite.InnerHtml = rm.GetString("Admin_PlaceUC_lbWebsite", ci);
            lbPicture.InnerHtml = rm.GetString("Admin_PlaceUC_lbPicture", ci);
            lbDescription.InnerHtml = rm.GetString("Admin_PlaceUC_lbDescription", ci);
            showInMap.Text = rm.GetString("Admin_PlaceUC_lbShowPoint", ci);
            lbSamePlaces.InnerHtml = rm.GetString("Admin_PlaceUC_lbSamePlaces", ci);
        }


        #region ICallbackEventHandler Members

        string callBackData;
        public string GetCallbackResult()
        {
            return callBackData;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            string[] necessaryData = eventArgument.Split('@');
            switch (necessaryData[0])
            {
                case "PlacePoint":
                    callBackData = "PlacePoint";
                    if (Session["mobimap_placePoint"] != null)
                        callBackData += "@" + (string)Session["mobimap_placePoint"];
                    else
                        callBackData += "@@";

                    //Lay dia diem trung voi dia diem dinh them
                    if (necessaryData.Length == 3)
                    {
                        string sameplaces = GetSamePlaces(necessaryData[1], necessaryData[2]);
                        callBackData += sameplaces;
                    }

                    
                    break;
                case "PlaceCoor":
                    callBackData = "PlaceCoor";
                    string latlong = GetPlaceCoor(necessaryData[1], necessaryData[2]);
                    callBackData += "@" + latlong;
                    //txtService_Latitude.Value = latlong;
                    break;
            }

        }

        #endregion

        public string GetSamePlaces(string placeType, string address)
        {
            IList<PlaceInfo> lstPlace = new List<PlaceInfo>();
            address = StringHelper.StandardizeString(address);
            if(address == string.Empty)
                return "@@";

            int type = int.Parse(placeType);
            address = StringHelper.GetSearchString(address, AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["abbr_file"]);
            lstPlace = PlaceEx.GetPlaceByTypeAndAddress(type, address);

            string strListPlace = string.Empty;
            int numRows = lstPlace.Count;
            if (lstPlace.Count > 5)
                numRows = 5;

            for (int i = 0; i < numRows; i++)
            {
                strListPlace += "@";
                strListPlace += lstPlace[i].FullName + "$" + lstPlace[i].Address + "$" + lstPlace[i].ID;
            }

            return strListPlace;
        }
             
        public string GetPlaceCoor(string pointX, string pointY)
        {
            int xx = int.Parse(pointX);
            int yy = int.Parse(pointY);

            MapInfoEx map = (MapInfoEx)Session["MAP"];
            Point pxCenter = new Point(map.Size.Width / 2, map.Size.Height / 2);
            Point curPoint = Point.Empty;
            curPoint.X = map.MapCenter.X - (pxCenter.X - xx);
            curPoint.Y = map.MapCenter.Y - (pxCenter.Y - yy);

            Coordinate coorPoint = LocateConvert.ToGeographicCoordinate(curPoint, map.Zoom);
            Session["mobimap_placeCoor"] = new PointF((float)coorPoint.Latitude,
                                                        (float)coorPoint.Longitude);

            return (coorPoint.Latitude.ToString("0.000000") + "@"
                            + coorPoint.Longitude.ToString("0.000000"));
        }
    }
}